/*
 *---------------------------------------------------------------------------
 *
 * Copyright (c) 2020, 2021 Analog Devices, Inc. All Rights Reserved.
 * This software is proprietary to Analog Devices, Inc.
 * and its licensors.By using this software you agree to the terms of the
 * associated Analog Devices Software License Agreement.
 *
 *---------------------------------------------------------------------------
 */

#ifndef MAIN_H
#define MAIN_H

#include <stdbool.h>
#include "adin1110.h"
#include "boardsupport.h"

/* Extra 4 bytes for FCS and 2 bytes for the frame header and up to 8 for timestamp */
#define MAX_FRAME_BUF_SIZE  (MAX_FRAME_SIZE + 4 + 2 + 8)

/* Payload values. */
#define ETH_PAYLOAD_IDX     (14)
#define SYNC_MESSAGE        (0x56594E43)  // "SYNC"
#define FWUP_MESSAGE        (0x46575550)  // "FolloW UP"
#define RESP_MESSAGE        (0x52455350)  // "RESP"

#define MAC_ADDR_0_0        (0x00)
#define MAC_ADDR_0_1        (0xE0)
#define MAC_ADDR_0_2        (0x22)
#define MAC_ADDR_0_3        (0xFE)
#define MAC_ADDR_0_4        (0xDA)
#define MAC_ADDR_0_5        (0xC9)

#define MAC_ADDR_1_0        (0x00)
#define MAC_ADDR_1_1        (0xE0)
#define MAC_ADDR_1_2        (0x22)
#define MAC_ADDR_1_3        (0xFE)
#define MAC_ADDR_1_4        (0xDA)
#define MAC_ADDR_1_5        (0xCA)

/* Synchronization state machine states. */
typedef enum
{
  SYNC_MACHINE_IDLE,
  SYNC_MACHINE_WAIT,   /*!< Waiting for SYNC & FOLLOW-UP messages.                  */
  SYNC_MACHINE_ASYNC,  /*!< Messages received but clock are not synchronized yet.   */
  SYNC_MACHINE_SYNCED, /*!< Local clock is synchronized to reference.               */
} syncMachineState_e;

/* Synchronization statistics tracked by the state machine. */
typedef struct
{
    uint32_t                slamCounter;
    uint32_t                syncCounter;
    uint32_t                syncCycleCounter;
    uint32_t                timeOffset;
    uint32_t                addend;
    bool                    syncState;
    adi_mac_TsTimespec_t    egressTime;
    adi_mac_TsTimespec_t    ingressTime;
} syncStatistics_t;

uint8_t macAddr[2][6] = {
    {MAC_ADDR_0_0, MAC_ADDR_0_1, MAC_ADDR_0_2, MAC_ADDR_0_3, MAC_ADDR_0_4, MAC_ADDR_0_5},
    {MAC_ADDR_1_0, MAC_ADDR_1_1, MAC_ADDR_1_2, MAC_ADDR_1_3, MAC_ADDR_1_4, MAC_ADDR_1_5},
};

uint8_t testFrame[MAX_FRAME_SIZE] =
{   
    MAC_ADDR_0_0, MAC_ADDR_0_1, MAC_ADDR_0_2, MAC_ADDR_0_3, MAC_ADDR_0_4, MAC_ADDR_0_5,
    MAC_ADDR_1_0, MAC_ADDR_1_1, MAC_ADDR_1_2, MAC_ADDR_1_3, MAC_ADDR_1_4, MAC_ADDR_1_5,
    0x08, 0x00,
    0x00, 0x00, 0x00, 0x00,
    0xC5, 0xDA, 0x48, 0x9A, 0x67, 0xA0, 0xBE, 0x3B,
    0x44, 0x64, 0xE1, 0xF3, 0x7E, 0x1D, 0x5F, 0x6C,
    0x7B, 0x56, 0xA1, 0xFB, 0xCF, 0xEE, 0xFD, 0xCE,
    0xAE, 0xAC, 0xA7, 0xE0, 0x9F, 0x17, 0x62, 0xA7,
    0x39, 0x27, 0xA4, 0xDC, 0xD9, 0x61, 0xA0, 0x20,
    0xB4, 0x6F, 0x31, 0x69, 0x01, 0xA0, 0x75, 0xCA,
    0xAD, 0x42, 0x0C, 0xC7, 0xF3, 0x7E, 0xF2, 0x62,
    0x27, 0x5A, 0x19, 0xF5, 0xFB, 0xD7, 0x3F, 0x9B,
    0xB1, 0x2A, 0x4F, 0xDD, 0x95, 0x04, 0xB2, 0x46,
    0x2B, 0x26, 0x2C, 0x6F, 0x4A, 0x02, 0xA5, 0xD0,
    0xFE, 0x6F, 0xAB, 0xC6, 0xB6, 0x5B, 0x2B, 0xAA,
    0x94, 0xB6, 0xDC, 0x42, 0x48, 0x81, 0x27, 0x9C,
    0xA1, 0xBD, 0x10, 0x6B, 0xD1, 0x03, 0x14, 0xDF,
    0x0C, 0xD9, 0x9A, 0xE1, 0x92, 0x3B, 0x02, 0xAF,
    0xD8, 0x46, 0x3F, 0xC1, 0x83, 0xB9, 0x85, 0xAF,
    0x9B, 0x2C, 0x7B, 0x01, 0x10, 0xD2, 0xED, 0x62,
    0xBD, 0x52, 0x5F, 0xDA, 0xF7, 0x19, 0x5D, 0x46,
    0x82, 0x6F, 0xE2, 0x7E, 0xC7, 0xCC, 0xFC, 0x3A,
    0x32, 0xF3, 0xF5, 0x8F, 0xC8, 0x1A, 0x41, 0x73,
    0xB7, 0x41, 0x8C, 0x1F, 0x24, 0x92, 0xDB, 0x36,
    0xEB, 0xAE, 0x41, 0xAF, 0xF6, 0x76, 0x75, 0x59,
    0xEC, 0x28, 0xD0, 0x5A, 0x85, 0x66, 0xE0, 0xDC,
    0x4E, 0xE7, 0x83, 0x51, 0x44, 0xE2, 0xD6, 0xE0,
    0xB4, 0x0C, 0xCB, 0x4E, 0xB9, 0x45, 0xCC, 0xF5,
    0xB5, 0xCD, 0xB2, 0xCB, 0xD7, 0xE0, 0x9A, 0x41,
    0xDE, 0x5A, 0x14, 0xA0, 0x85, 0x54, 0x6E, 0xD9,
    0x43, 0xC6, 0xF6, 0x15, 0xC7, 0xF8, 0x90, 0x9D,
    0x38, 0x62, 0x56, 0xF6, 0x86, 0xA3, 0x91, 0x0D,
    0xA9, 0xDE, 0xD0, 0x22, 0x4E, 0x37, 0x1D, 0x92,
    0x33, 0x2A, 0x99, 0x24, 0x94, 0xD1, 0xE8, 0x17,
    0xF3, 0x12, 0x8C, 0x9F, 0x98, 0x62, 0xBB, 0x9A,
    0x49, 0xE0, 0x9A, 0xF9, 0x19, 0xF9, 0x5E, 0x9E,
    0x4F, 0x7B, 0x81, 0xE6, 0x16, 0x40, 0x18, 0x04,
    0x4E, 0xD4, 0xA6, 0x47, 0x64, 0xA5, 0x60, 0x5C,
    0xFA, 0x0A, 0xA6, 0x82, 0x06, 0x81, 0x02, 0x11,
    0x43, 0xFD, 0xBF, 0x3C, 0x68, 0x7E, 0x20, 0x6E,
    0xB4, 0x50, 0x82, 0xEE, 0x20, 0x5E, 0x78, 0xB2,
    0x3B, 0x65, 0xE3, 0x74, 0x33, 0xA2, 0x34, 0x21,
    0xB4, 0xD0, 0xCB, 0xD2, 0xF2, 0x46, 0x21, 0x82,
    0xF1, 0x33, 0x84, 0x52, 0x5F, 0x3A, 0x97, 0xC2,
    0x15, 0x00, 0xF4, 0x7E, 0x8F, 0xB8, 0xFD, 0x67,
    0x94, 0x08, 0xB5, 0x56, 0x3A, 0x59, 0xFC, 0x38,
    0xED, 0xA2, 0x32, 0x4D, 0xB2, 0x30, 0x68, 0x86,
    0x42, 0xF4, 0xB6, 0x99, 0x91, 0xAF, 0x3A, 0x6E,
    0x7D, 0xB4, 0xD5, 0xBD, 0x6D, 0x1A, 0xEE, 0x31,
    0x5E, 0x83, 0x18, 0x9F, 0x31, 0x80, 0x92, 0x65,
    0x5D, 0x3A, 0xC3, 0x66, 0x70, 0x0D, 0x34, 0x51,
    0xF6, 0x9F, 0xD0, 0x2C, 0xE7, 0xB0, 0xA2, 0xEF,
    0x75, 0x66, 0x41, 0x0F, 0xD3, 0xCC, 0xE0, 0x93,
    0x8D, 0x19, 0xC8, 0x73, 0x22, 0x44, 0x6D, 0x1F,
    0xB6, 0xED, 0x78, 0xCF, 0xDC, 0x53, 0x6B, 0x6D,
    0x3A, 0x40, 0xCA, 0x6F, 0xBD, 0x00, 0xB4, 0x41,
    0x6D, 0xD5, 0x28, 0x98, 0x57, 0x6E, 0xE3, 0x12,
    0x1E, 0x46, 0x51, 0xF0, 0xE9, 0xBA, 0x16, 0x4A,
    0x40, 0xE1, 0x43, 0x8D, 0x5A, 0x85, 0x1B, 0x65,
    0x0A, 0x36, 0x99, 0xF1, 0xD3, 0x55, 0xCF, 0x55,
    0x36, 0xB1, 0x68, 0xEB, 0xA6, 0xBB, 0x7A, 0x96,
    0x18, 0x51, 0xAF, 0x6D, 0x2C, 0x91, 0x6D, 0x81,
    0x60, 0x67, 0xD0, 0x8B, 0x92, 0x3D, 0x9E, 0xC9,
    0x52, 0x4C, 0x2F, 0x3F, 0x57, 0xB6, 0xE9, 0x6E,
    0xCF, 0x32, 0x46, 0x53, 0xD3, 0x4F, 0x04, 0x51,
    0x00, 0xD6, 0x5F, 0x45, 0xE8, 0xA1, 0xBB, 0x7A,
    0x52, 0x3C, 0xBF, 0x96, 0x31, 0xDD, 0x8B, 0xB9,
    0x54, 0xB8, 0x27, 0x09, 0xA8, 0x76, 0x70, 0x5B,
    0xF6, 0x85, 0x41, 0x10, 0x9D, 0x1E, 0xEF, 0x71,
    0x99, 0x9D, 0xD8, 0x80, 0x4D, 0x2D, 0xEC, 0x09,
    0x47, 0x1C, 0xB8, 0xED, 0x3B, 0x9F, 0x1A, 0x73,
    0x14, 0x5B, 0x08, 0xBA, 0x59, 0x31, 0x8F, 0x84,
    0x39, 0xD6, 0x98, 0x5A, 0xA1, 0x55, 0xFF, 0x4A,
    0x82, 0x74, 0x9C, 0x5D, 0xC3, 0x44, 0xEF, 0x96,
    0x84, 0xB2, 0x78, 0x65, 0x0E, 0x05, 0x7E, 0x3D,
    0xB5, 0xA7, 0x1F, 0x7A, 0x72, 0xA6, 0x9C, 0x15,
    0x5F, 0x39, 0xBC, 0x37, 0x2F, 0xE7, 0x2D, 0x79,
    0xFB, 0xE7, 0x0F, 0x8B, 0x22, 0x9B, 0x64, 0x0E,
    0x0C, 0xCD, 0xE7, 0x90, 0x5E, 0x42, 0x29, 0x80,
    0x20, 0xAF, 0x25, 0x21, 0x84, 0x48, 0x92, 0x3D,
    0xE9, 0x8B, 0x4F, 0x75, 0x98, 0x0E, 0xA0, 0xC7,
    0x3E, 0xC6, 0xC4, 0x3F, 0x7E, 0x8A, 0x5D, 0xB3,
    0x88, 0xE2, 0xB4, 0xF7, 0xDE, 0x55, 0x02, 0x9F,
    0x0E, 0xE2, 0x6B, 0xB3, 0x19, 0xBE, 0x00, 0xE4,
    0xE2, 0x56, 0x82, 0xF5, 0x84, 0x35, 0x7D, 0xFC,
    0x14, 0x81, 0xB5, 0x88, 0xC4, 0x82, 0x79, 0x58,
    0x33, 0x09, 0x45, 0x37, 0xDF, 0x11, 0xB7, 0x1C,
    0x09, 0x8D, 0x92, 0x4A, 0xF7, 0x4F, 0x39, 0xAD,
    0x23, 0xE0, 0x31, 0xEA, 0xFA, 0x4A, 0x6C, 0x4B,
    0xFB, 0x77, 0xE8, 0x11, 0xC9, 0x2A, 0xF2, 0x5D,
    0x91, 0x2A, 0x95, 0xB7, 0x9E, 0x9C, 0xA9, 0x1E,
    0x84, 0xA5, 0xB5, 0x78, 0xF8, 0xF7, 0x9B, 0x26,
    0x93, 0x0B, 0x91, 0x4C, 0x1D, 0x9E, 0x7C, 0xB0,
    0x4B, 0x05, 0xB6, 0x59, 0xAD, 0x55, 0x1C, 0x31,
    0x0F, 0x6E, 0x52, 0xBD, 0xDD, 0x09, 0x60, 0x39,
    0x88, 0x41, 0x10, 0xB3, 0xE1, 0x37, 0x6E, 0x20,
    0x1A, 0xC1, 0x7E, 0xB1, 0x82, 0x27, 0xEE, 0xC2,
    0x25, 0xF7, 0xF8, 0x4F, 0xB1, 0x59, 0xBD, 0x6C,
    0x0D, 0x51, 0x83, 0x0D, 0xB7, 0x95, 0xB1, 0x12,
    0x7D, 0x82, 0xE1, 0xCC, 0x68, 0xD0, 0x6A, 0xE9,
    0x71, 0x8A, 0xEA, 0xEC, 0x63, 0x14, 0x79, 0x72,
    0x8D, 0xD1, 0x0F, 0x98, 0xA1, 0x08, 0x90, 0x48,
    0x09, 0x78, 0xBB, 0x33, 0x40, 0xE7, 0x5C, 0x67,
    0x68, 0xC8, 0xE0, 0xE0, 0xA0, 0xA3, 0x11, 0x65,
    0x1E, 0xEB, 0xE9, 0x69, 0xFC, 0x21, 0xD2, 0xDD,
    0xE1, 0xB1, 0x4D, 0x26, 0xEF, 0x45, 0xE9, 0xAD,
    0x11, 0x39, 0x41, 0xE4, 0xF1, 0x36, 0xDF, 0xF7,
    0x5F, 0x68, 0xBF, 0xDA, 0xC6, 0x23, 0xE8, 0x20,
    0x7B, 0xF5, 0x3B, 0xC9, 0x21, 0x07, 0xBA, 0x7C,
    0xDF, 0xCE, 0x01, 0x0D, 0xF2, 0xB1, 0xE5, 0xD5,
    0xE2, 0xAA, 0x20, 0x3C, 0x9B, 0x03, 0x81, 0xE2,
    0x93, 0xB0, 0x50, 0x5F, 0x1A, 0xD8, 0x8B, 0x40,
    0x74, 0x0F, 0x6E, 0xDE, 0xB4, 0x77, 0x9A, 0x91,
    0x27, 0xAA, 0x1E, 0x73, 0xD3, 0x95, 0x87, 0xE0,
    0x32, 0xD5, 0xA5, 0xAC, 0x64, 0x86, 0x3E, 0x1C,
    0x6F, 0xAB, 0xA1, 0x66, 0x78, 0xB2, 0xE2, 0xE3,
    0x1C, 0xAB, 0xAB, 0xD4, 0x3E, 0xEE, 0x79, 0x0E,
    0x26, 0xD0, 0x0F, 0xC8, 0xC2, 0xDF, 0x12, 0xDC,
    0x9B, 0x7C, 0x39, 0x40, 0x74, 0x22, 0x57, 0x64,
    0x00, 0xF7, 0xBB, 0xFA, 0xB4, 0x7E, 0xF6, 0xE2,
    0x40, 0x6A, 0x92, 0x4E, 0x54, 0xD7, 0x65, 0xE1,
    0x7D, 0xBD, 0xBB, 0xC5, 0x07, 0xEA, 0xEF, 0x4B,
    0xC5, 0x4F, 0xEB, 0x2D, 0x3F, 0x26, 0xE9, 0x33,
    0xBB, 0x8B, 0x2C, 0xBE, 0x88, 0x99, 0x86, 0x05,
    0x10, 0x13, 0xEA, 0x5C, 0x1C, 0xDF, 0x59, 0x63,
    0xEA, 0xBD, 0x43, 0xAC, 0xB3, 0x47, 0x5A, 0x31,
    0x9F, 0x6F, 0x28, 0x1B, 0xCC, 0xB6, 0xD5, 0x50,
    0x2B, 0xA0, 0xB0, 0xB2, 0xB5, 0x32, 0x94, 0xA6,
    0x7B, 0xC6, 0x45, 0x97, 0x8F, 0xA2, 0xD8, 0x5A,
    0x45, 0x6E, 0xAF, 0xBA, 0xE1, 0xF2, 0xB5, 0x73,
    0xC0, 0x7E, 0xE2, 0xDB, 0xAE, 0x10, 0xFE, 0xCC,
    0x18, 0xCB, 0xFE, 0x2E, 0x3A, 0xD4, 0x67, 0x81,
    0xEA, 0x8E, 0xBD, 0x74, 0x28, 0x9D, 0xD8, 0x28,
    0x00, 0x2D, 0x3B, 0xAC, 0x72, 0xF4, 0xD1, 0x12,
    0x68, 0xC2, 0x1D, 0xF3, 0x01, 0x04, 0x78, 0x64,
    0x7F, 0x66, 0x0F, 0x1C, 0xF1, 0x9D, 0x2B, 0xDD,
    0x62, 0xD7, 0x3C, 0x0A, 0x94, 0x79, 0x48, 0xB5,
    0xEE, 0xEA, 0x65, 0xA8, 0x26, 0x8C, 0x79, 0x2B,
    0xDD, 0x17, 0xE0, 0xF6, 0xE6, 0xAC, 0x7B, 0xE4,
    0x65, 0x9C, 0x2E, 0xB7, 0x89, 0xE2, 0x84, 0x17,
    0xA0, 0xD6, 0x95, 0xFE, 0xA9, 0x54, 0x6A, 0x28,
    0x52, 0xBB, 0xED, 0xCF, 0xE3, 0xE9, 0xB4, 0x53,
    0x44, 0xCC, 0x22, 0x04, 0x66, 0x4F, 0x8D, 0x1E,
    0xE1, 0x58, 0xC5, 0x60, 0x39, 0x26, 0xD0, 0xC5,
    0x00, 0x4C, 0xF1, 0xB3, 0x07, 0xCA, 0x89, 0x53,
    0xB0, 0x58, 0x67, 0x93, 0x07, 0x55, 0x4F, 0xAA,
    0x86, 0x9A, 0x9E, 0x45, 0x16, 0xA0, 0xC9, 0x14,
    0xDB, 0x30, 0x93, 0x21, 0x31, 0x33, 0x91, 0x7F,
    0xBA, 0x68, 0x3D, 0x19, 0xD5, 0x94, 0xAA, 0x20,
    0xDB, 0xBA, 0x9E, 0xB5, 0xE6, 0x3B, 0xA6, 0x50,
    0xF7, 0x35, 0x58, 0xEB, 0x7D, 0xA4, 0xC4, 0xF8,
    0x65, 0x9B, 0x8B, 0xF4, 0xF2, 0x52, 0x0F, 0x83,
    0x29, 0xA4, 0x99, 0x55, 0x0E, 0x1D, 0x26, 0x4F,
    0x28, 0x21, 0x73, 0x33, 0x9E, 0x32, 0x3F, 0xF5,
    0xC2, 0x36, 0x65, 0x57, 0x89, 0x54, 0xE1, 0xC1,
    0x61, 0x94, 0xE7, 0xBF, 0x64, 0xCF, 0xD0, 0x1D,
    0x19, 0xE3, 0x45, 0x44, 0x02, 0xD0, 0xAA, 0xF4,
    0x99, 0xDB, 0x31, 0xAD, 0xD5, 0xB5, 0xC1, 0x9A,
    0xA6, 0xC0, 0x3F, 0x20, 0x29, 0x58, 0x37, 0x03,
    0x0A, 0x18, 0xA7, 0x00, 0xD1, 0xA1, 0xA8, 0x73,
    0x00, 0x22, 0x91, 0x04, 0x3B, 0x2B, 0x11, 0xB3,
    0xD5, 0x50, 0x5E, 0x8E, 0x6A, 0xD4, 0xB4, 0x69,
    0x0E, 0xFA, 0x69, 0xE4, 0x80, 0x3B, 0x30, 0xA9,
    0x12, 0x84, 0x84, 0xD9, 0x8E, 0x4F, 0x3E, 0x87,
    0xB8, 0x76, 0x62, 0xA5, 0x4B, 0x10, 0xCD, 0xF2,
    0x8C, 0xF4, 0x88, 0x0F, 0xDF, 0xD7, 0x12, 0x46,
    0x35, 0xB9, 0x53, 0xD1, 0x0A, 0x7D, 0xAC, 0xFA,
    0x0B, 0xDC, 0xDC, 0x96, 0x76, 0xE2, 0xB4, 0xCC,
    0x5B, 0x44, 0x98, 0x03, 0xAA, 0xDF, 0x50, 0xBC,
    0x6B, 0x55, 0xCF, 0x3C, 0x7F, 0xB9, 0x7A, 0x7B,
    0x64, 0x71, 0x5F, 0x9E, 0x82, 0x2F, 0xF5, 0x2B,
    0x23, 0xC9, 0x57, 0xC7, 0x6D, 0x73, 0xC9, 0x37,
    0xCC, 0x04, 0x21, 0xE3, 0x2E, 0xF8, 0x09, 0xD4,
    0x50, 0xE6, 0xD6, 0x8F, 0x38, 0x33, 0x58, 0x75,
    0xB2, 0x41, 0x37, 0x62, 0x91, 0xA2, 0x2E, 0x2D,
    0x1D, 0x51, 0xFA, 0x42, 0xA4, 0xA2, 0xBD, 0x01,
    0x3E, 0x09, 0x99, 0x52, 0x9F, 0x9B, 0xCA, 0x31,
    0xC5, 0xF4, 0x75, 0x5A, 0xA2, 0xC8, 0xF7, 0xF6,
    0xA1, 0xA1, 0x92, 0xFF, 0xD1, 0x72, 0x36, 0x34,
    0x21, 0xCD, 0x2E, 0x3F, 0xE3, 0x50, 0x60, 0x83,
    0x7C, 0xDA, 0xDE, 0x40, 0xA3, 0x49, 0x24, 0x79,
    0xA5, 0x53, 0x52, 0x62, 0xE7, 0xD4, 0x35, 0x98,
    0x1B, 0xFA, 0x3D, 0x13, 0x0F, 0x7A, 0x24, 0x75,
    0x3B, 0x05, 0x4C, 0xE2, 0x2B, 0x55, 0x5C, 0x99,
    0x83, 0x45, 0x4A, 0xA3, 0x04, 0x93, 0xBE, 0x10,
    0xD9, 0xD1, 0x44, 0x21, 0x7F, 0xED, 0x25, 0xAA,
    0xF5, 0x2F, 0xA7, 0x0D, 0x70, 0x2E, 0x40, 0x5F,
    0x23, 0xF5, 0x12, 0xAD, 0x15, 0xEC, 0x96, 0xDA,
    0x20, 0xBB, 0xC2, 0x9A, 0x43, 0xB8, 0xA0, 0x7C,
    0x2B, 0xA2, 0x07, 0x30, 0x5E, 0x62, 0xE8, 0x45,
    0xF5, 0x85, 0xB0, 0x53, 0xCB, 0x5B, 0xF1, 0x45,
    0xBA, 0x50, 0x4F, 0x8A
};


#endif /* MAIN_H */
